<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="快递单号" prop="expressNo">
        <el-input v-model="queryParams.expressNo" placeholder="请输入快递单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.ORDER_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="支付状态" prop="paid">
        <el-select v-model="queryParams.paid" placeholder="请选择支付状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.PAY_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="金料类型" prop="goldType">
        <el-select v-model="queryParams.goldType" placeholder="请选择金料类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.RECYCLE_GOLD_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="回收方式" prop="onlineOrder">
        <el-select v-model="queryParams.onlineOrder" placeholder="请选择回收方式" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.ONLINE_OR_STORE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付类型" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.RECYCLE_PAY_TYPE)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>

      <el-form-item label="出库编号" prop="outboundNo">
        <el-input v-model="queryParams.outboundNo" placeholder="请输入出库编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item label="完成时间" prop="finishTime">
        <el-date-picker v-model="queryParams.finishTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                   v-hasPermi="['system:order-info:create']">新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['system:order-info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="id" align="center" prop="id" />
<!--      <el-table-column label="用户id" align="center" prop="uid" />-->
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="用户姓名" align="center" prop="name" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="快递单号" align="center" prop="expressNo" />
      <el-table-column label="回收方式" align="center" prop="onlineOrder">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.ONLINE_OR_STORE" :value="scope.row.onlineOrder" />
        </template>
      </el-table-column>
      <el-table-column label="金料类型" align="center" prop="goldType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.RECYCLE_GOLD_TYPE" :value="scope.row.goldType" />
        </template>
      </el-table-column>
      <el-table-column label="克重" align="center" prop="goldWeight" />
      <el-table-column label="回收金价" align="center" prop="goldPrice" />
      <el-table-column label="预估金额" align="center" prop="orderExpectAmount" />
      <el-table-column label="服务费（%）" align="center" prop="serviceFee" />
      <el-table-column label="支付类型" align="center" prop="payType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.RECYCLE_PAY_TYPE" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" prop="payAmount" />
      <el-table-column label="支付状态" align="center" prop="paid">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.PAY_STATUS" :value="scope.row.paid" />
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.ORDER_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="出库编号" align="center" prop="outboundNo" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding" width="150">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="openForm(scope.row.id)"
                     v-hasPermi="['system:order-info:update']">详情</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:order-info:delete']">删除</el-button>-->
          <el-button v-if="scope.row.status != 4 && scope.row.status != 3" size="mini" type="text" icon="el-icon-edit" @click="openCancelOrder(scope.row)"
                     v-hasPermi="['system:order-info:cancelOrder']">取消订单</el-button>
          <el-button v-if="scope.row.onlineOrder === 1 && scope.row.status === 1" size="mini" type="text" icon="el-icon-edit" @click="handleConfirmTake(scope.row)"
                     v-hasPermi="['system:order-info:confirmTake']">确认收货</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <OrderInfoForm ref="formRef" @success="getList" />

    <el-dialog title="取消订单" :visible.sync="cancelDialog" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef2" :model="formData" :rules="formRules2" v-loading="formLoading" label-width="100px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="formData.orderNo" placeholder="请输入组" disabled/>
        </el-form-item>
        <el-form-item label="支付金额" prop="payAmount">
          <el-input v-model="formData.payAmount" placeholder="请输入key值" disabled/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注" maxlength="200"/>
        </el-form-item>
        <el-form-item label="凭证图片" prop="cancelImg">
          <ImageUpload v-model="formData.cancelImg" :limit="2"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleCancelOrder" :disabled="formLoading">确 定</el-button>
        <el-button @click="cancelDialog = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import * as OrderInfoApi from '@/api/system/orderinfo';
import OrderInfoForm from './OrderInfoForm.vue';
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "OrderInfo",
  components: {
    OrderInfoForm,
    ImageUpload
  },
  data() {
    return {
      cancelDialog: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      formLoading: false,
      // 总条数
      total: 0,
      // 回收订单列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        uid: null,
        orderNo: null,
        expressNo: null,
        outboundNo: null,
        phone: null,
        status: null,
        paid: null,
        finishTime: [],
        goldType: null,
        onlineOrder: null,
        payType: null,
        createTime: [],
      },
      formData: {
        id: null,
        remark: null,
        cancelImg: null
      },
      // 表单校验
      formRules2: {
        remark: [{ required: true, message: '不能为空', trigger: 'blur' }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await OrderInfoApi.getOrderInfoPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除回收订单编号为"' + id + '"的数据项?')
      try {
        await OrderInfoApi.deleteOrderInfo(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有回收订单数据项?');
      try {
        this.exportLoading = true;
        const data = await OrderInfoApi.exportOrderInfoExcel(this.queryParams);
        this.$modal.msgSuccess("提交下载申请成功，稍后在下载记录中查看");
        // this.$download.excel(data, '回收订单.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },

    async openCancelOrder(row) {
      this.cancelDialog = true
      this.formData = row
    },

    /** 取消订单 按钮操作 */
    async handleCancelOrder() {
      await this.$refs["formRef2"].validate();
      this.formLoading = true;
      await this.$modal.confirm('是否确认取消订单编号为"' + this.formData.orderNo + '"的数据项?')
      try {
        await OrderInfoApi.cancelOrder(this.formData);
        await this.getList();
        this.$modal.msgSuccess("取消订单成功");
        this.cancelDialog = false
        this.formLoading = false;
      } catch {}
    },

    /** 确认收货 按钮操作 */
    async handleConfirmTake(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认收货订单编号为"' + id + '"的数据项?')
      try {
        await OrderInfoApi.confirmTake(id);
        await this.getList();
        this.$modal.msgSuccess("确认收货成功");
      } catch {}
    },

  }
};
</script>
